package bl.auxiliary;

import java.io.Serializable;
import java.util.ArrayList;

import exception.FavorMaxException;
import exception.FavorRepeatException;
import exception.NotFavorException;

import po.MemberPO;

public class MemberFavorList implements Serializable {
	
	private static final long serialVersionUID = 3047508667473196266L;

	private static final int MAX_FAVOR_QUANTITY = 20;
	
	private MemberPO memberPO;

	public MemberFavorList() {
		this.memberPO = new MemberPO();
	}
	
	public MemberFavorList(MemberPO memberPO) {
		this.memberPO = memberPO;
	}
	
	public boolean favor(int bookID) throws FavorMaxException, FavorRepeatException {
		ArrayList<Integer> list = memberPO.getFavorList();
		if (list == null) {
			return false;
		}
		if (list.size() >= MAX_FAVOR_QUANTITY) {
			throw new FavorMaxException();
		}
		for (int i = 0; i < list.size(); i++) {
			int id = list.get(i);
			if (id == bookID) {
				throw new FavorRepeatException();
			}
		}
		list.add(bookID);
		return true;
	}
	
	public boolean removeFavor(int bookID) throws NotFavorException {
		ArrayList<Integer> list = memberPO.getFavorList();
		if (list == null) {
			return false;
		}
		Integer id = new Integer(bookID);
		boolean remove = list.remove(id);
		if (!remove) {
			throw new NotFavorException();
		}
		return true;
	}

}
